package com.daffodil.workable.entity;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.*;

import org.hibernate.annotations.GenericGenerator;

import com.daffodil.core.annotation.Hql;
import com.daffodil.core.annotation.Hql.Logical;
import com.daffodil.core.entity.BaseEntity;
import com.daffodil.util.StringUtils;
import com.fasterxml.jackson.annotation.JsonFormat;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * -计划任务对象
 * @author yweijian
 * @date 2022-04-06
 * @version 1.0
 * @description
 */
@Data
@EqualsAndHashCode(callSuper = true)
@Entity
@Table(name = "wok_task")
public class WorkableTask extends BaseEntity<String> {

    private static final long serialVersionUID = -7961347474694043234L;

    /** 主键编号 */
    @Id
    @GeneratedValue(generator = "uuid")
    @GenericGenerator(name = "uuid", strategy = "uuid")
    @Column(name = "id")
    private String id;

    /** 任务名称 */
    @Column(name = "task_name", length = 128)
    @Hql(type = Logical.LIKE)
    @NotBlank(message = "任务名称不能为空")
    @Size(min = 1, max = 128, message = "任务名称长度限制为1~128个字符")
    private String taskName;
    
    /** 任务状态 */
    @Column(name = "status")
    @Hql(type = Logical.EQ)
    private String status;
    
    /** 任务排序 */
    @Column(name = "order_num")
    @PositiveOrZero(message = "任务排序只能是正整数或零")
    private Long orderNum;

    /** 创建者 */
    @Column(name = "create_by")
    @Hql(type = Logical.EQ)
    private String createBy;

    /** 创建时间 */
    @Column(name = "create_time")
    @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss")
    private Date createTime;

    /** 更新者 */
    @Column(name = "update_by")
    @Hql(type = Logical.EQ)
    private String updateBy;

    /** 更新时间 */
    @Column(name = "update_time")
    @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss")
    private Date updateTime;

    /** 备注 */
    @Column(name = "remark")
    private String remark;
    
    /** 部署时间 */
    @Column(name = "deploy_time")
    @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss")
    private Date deployTime;
    
    /** 部署ID（源taskId） */
    @Column(name = "deploy_id")
    private String deployId;
    
    /** 主要版本号 */
    @Column(name = "major")
    private Integer major;
    
    /** 次要版本号 */
    @Column(name = "minor")
    private Integer minor;
    
    /** 补丁版本号 */
    @Column(name = "patch")
    private Integer patch;
    
    /** 构建版本号 */
    @Column(name = "build")
    private Integer build;
    
    @Transient
    private String version;
    
    public void setTaskVersion(Version version) {
        this.major = version.getMajor();
        this.minor = version.getMinor();
        this.patch = version.getPatch();
        this.build = version.getBuild();
    }
    
    public String getVersion() {
        return new Version(this.major, this.minor, this.patch, this.build).toString();
    }
    
    @Data
    @AllArgsConstructor
    public static class Version implements Serializable{

        private static final long serialVersionUID = 1L;

        /** 主要版本号 */
        private Integer major;
        
        /** 次要版本号 */
        private Integer minor;
        
        /** 补丁版本号 */
        private Integer patch;
        
        /** 构建版本号 */
        private Integer build;

        @Override
        public String toString() {
            return StringUtils.format("{}.{}.{}.{}", major, minor, patch, build);
        }
    }
}
